package com.weiaibenpao.demo.chislim.sport.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.util.Log;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.weiaibenpao.demo.chislim.R;

import static com.umeng.socialize.bean.SHARE_MEDIA.QQ;

/**
 * Created by wlx on 2017/8/31.
 */

public class ShareUtils {
    private static final String SHARE_SUFFIX = "&shareJudge=y";
    private Context context;

    public ShareUtils(Context context) {
        this.context = context;
    }

    public void shareLink(String url,String distance){
        UMImage image = new UMImage(context, R.mipmap.ic_logo);
        final SHARE_MEDIA[] displayList = getShareMedias();
        new ShareAction((Activity) context).setDisplayList(displayList)
                .withText(context.getString(R.string.default_share_content))
                .withTitle("已完成"+distance+"公里，运动一身轻")
                .withTargetUrl(url+SHARE_SUFFIX)
                .withMedia(image)
                .setListenerList(umShareListener)
                .open();
    }

    public void shareLink(String url,String marathonTitle,boolean isFinished){
        UMImage image = new UMImage(context, R.mipmap.ic_logo);
        final SHARE_MEDIA[] displayList= getShareMedias();
        String shareTitle = marathonTitle;
        if(isFinished){
            shareTitle += context.getString(R.string.challenge_succeed);
        }else {
            shareTitle += context.getString(R.string.challenge_failed);
        }
        Log.e("shareUtils","url is "+url+SHARE_SUFFIX);
        new ShareAction((Activity) context).setDisplayList(displayList)
                .withText(context.getString(R.string.default_share_content))
                .withTitle(shareTitle)
                .withTargetUrl(url+SHARE_SUFFIX)
                .withMedia(image)
                .setListenerList(umShareListener)
                .open();
    }



    public void shareLink(String url, String title, String text){
        UMImage image = new UMImage(context, R.mipmap.ic_logo);
        /*UMWeb  web = new UMWeb(url);
        web.setTitle(title);//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription(text);//描述*/

        final SHARE_MEDIA[] displayList = getShareMedias();
        new ShareAction((Activity) context).setDisplayList(displayList)
                .withText(text)
                .withTitle(title)
                .withTargetUrl(url+SHARE_SUFFIX)
                .withMedia(image)
                .setListenerList(umShareListener)
                .open();
    }


    public void shareBitmap(Bitmap bitmap,String distance) {
        UMImage image = new UMImage(context, bitmap);
        final SHARE_MEDIA[] displayList = getShareMedias();
        new ShareAction((Activity) context).setDisplayList(displayList)
                .withText("东极圈，跑出不一样的自己!启迈斯为您助跑")
                .withTitle("已完成"+distance+"公里，运动一身轻")
                .withMedia(image)
                .setListenerList(umShareListener)
                .open();
    }

    /**
     * 使用ShareAction分享时：友盟奇葩之处：分享单纯本地图片（效果就是点击图片查看大图那种）
     * 1.如果设置了文字，分享到微信好友和朋友圈，那么分享的就是点进去跳友盟官网
     * 2. 但是如果不设置文字，只设置图片那么无法分享到QQ空间和新浪微博。。。
     * ----------项目中的友盟分享是5.2.1，估计是版本太老所致
     * @param bitmap
     * @param platform
     */
    public void shareBitmap(Bitmap bitmap, SHARE_MEDIA platform,String distance) {
        UMImage image = new UMImage(context, bitmap);
        UMImage thumb =  new UMImage(context, R.drawable.ic_launcher);
//        image.setThumb(thumb);
        if( platform == SHARE_MEDIA.WEIXIN || platform == SHARE_MEDIA.WEIXIN_CIRCLE ){
            new ShareAction((Activity) context)
                    .setPlatform(platform)
                    .withMedia(image)
                    .setCallback(umShareListener)
                    .share();
        }else {
            new ShareAction((Activity) context)
                    .setPlatform(platform)
                    .withText("东极圈，跑出不一样的自己!启迈斯为您助跑")
                    .withTitle("已完成"+distance+"公里，运动一身轻")
                    .withMedia(image)
                    .setCallback(umShareListener)
                    .share();
        }
    }


    @NonNull
    private SHARE_MEDIA[] getShareMedias() {
        return new SHARE_MEDIA[]
                {
                        SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA,
                        QQ, SHARE_MEDIA.QZONE
                };
    }

    /**
     * 分享回调
     */
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
        }
    };
}
